package com.utils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 获取咖啡库存并执行发送短信
 * @author: fanyouzhao
 * @date: 15:33 2021/5/31
 */
public class GetCoffee {
    public static void getCoffee(String phone,String name,String shop,String deptId) throws Exception {
        // 获得某门店商品的详情单
        String strUrl = "https://capistatic.luckincoffeecdn.com/resource/core/v2/product/list/static";
        //生椰系列的产品id数组
        String[] prodIdArr = {"1645", "1603", "1610", "1262", "1547", "1344", "1302"};
        // 发送Get请求
        String s = HttpRequestUtils.sendGet(strUrl, "deptId="+deptId);

        // 对返回的json字符串一层一层剥离处理。
        // json文件demo在文件夹resource/CoffeBackDemo.json中
        JSONObject jsonObject = JSONObject.fromObject(s);
        JSONObject content = jsonObject.getJSONObject("content");
        JSONArray commodityList = content.getJSONArray("commodityList");

        for (int i = 0; i < commodityList.size(); i++) {
            JSONObject tempCom = commodityList.getJSONObject(i);
            String kindName = tempCom.getString("kindName");
            JSONArray twoProductList = tempCom.getJSONArray("twoProductList");
            for (int j = 0; j < twoProductList.size(); j++) {
                JSONObject tempTwoProd = twoProductList.getJSONObject(j);
                String twoKindName = tempTwoProd.getString("twoKindName");
                JSONArray productList = tempTwoProd.getJSONArray("productList");
                for (int m = 0; m < productList.size(); m++) {
                    JSONObject tempProd = productList.getJSONObject(m);
                    String productId = tempProd.getString("productId");
                    String prodName = tempProd.getString("name");
                    // 产品库存
                    int maxAmount = tempProd.getInt("maxAmount");
                    for (String prodId : prodIdArr) {
                        if (prodId.equals(productId)) {
                            if (maxAmount > 0) {
                                System.out.println("当前'" + prodName + "'正在出售...");
                                AliMsm.sendMsg(phone, name, shop, "'"+prodName+"'");
                            } else {
                                System.out.println("'" + prodName + "'已售罄");
                            }
                        }
                    }
                }
            }
        }
        System.out.println("-------------------------------------");
    }
}
